package com.newbee.sound;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.util.Log;

public class SoundPlayer extends Sound
{

	private AudioTrack audioTrack;

	private MediaPlayer mMediaPlayer;

	public SoundPlayer()
	{

		mMediaPlayer = new MediaPlayer();
	}

	public void play(String fileName)
	{

		if (audioTrack != null)
		{

			audioTrack.stop();
			audioTrack.release();
		}
		if (audioTrack != null)
		{
			Log.d("fileName", "getState------------>" + audioTrack.getState());
		}

		File file = new File(Sound.DIR + fileName);

		byte[] music = null;
		try
		{
			if (file.exists())
			{
				InputStream is = new FileInputStream(file);
				int length = is.available();
				music = new byte[length];
				is.read(music);
				is.close();
				audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
						sampleRateInHz, channelConfig, audioFormat, length,
						AudioTrack.MODE_STREAM);

				audioTrack.write(music, 0, length);
				audioTrack.play();
			}

		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	public void stop()
	{
		if (audioTrack != null)
		{

			audioTrack.stop();
			audioTrack.release();
		}
	}

}
